package com.sunriz.motordrive.server.photoVoltaic.Impl;

import com.sunriz.motordrive.domain.DAO.PvHcInfoDAO;
import com.sunriz.motordrive.server.photoVoltaic.HcInfoConfigServer;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: HcInfoConfigImpl
 * Package: com.sunriz.motordrive.server.photoVoltaic.Impl
 * Description:
 *
 * @Author BuTang
 * @Create 2024/8/14 11:14
 * @Version 1.0
 */
@Slf4j
@Service
public class HcInfoConfigServerImpl implements HcInfoConfigServer {

    // 取消使用数据库
//    @Resource
//    PvHcMapper pvHcMapper;

    @Override
    public void addData(PvHcInfoDAO pvHcInfo) {
        String excelFilePath = "config.xlsx";

        try (InputStream fis = new FileInputStream(excelFilePath);
             Workbook workbook = new XSSFWorkbook(fis)) {

            // 获取工作表
            Sheet sheet = workbook.getSheet("PvHcInfo");
            // 在最后一行后面添加新行
            int newRowIdx = sheet.getLastRowNum() + 1;
            Row newRow = sheet.createRow(newRowIdx);

            newRow.createCell(0).setCellValue(pvHcInfo.address);
            newRow.createCell(1).setCellValue(pvHcInfo.name);
            newRow.createCell(2).setCellValue(pvHcInfo.signed);
            newRow.createCell(3).setCellValue(pvHcInfo.precision);
            newRow.createCell(4).setCellValue(pvHcInfo.numberOfBytes);
            newRow.createCell(5).setCellValue(pvHcInfo.readOrWrite);
            newRow.createCell(6).setCellValue(pvHcInfo.tableNumber);
            newRow.createCell(7).setCellValue(pvHcInfo.describe);

            // 保存更改到 Excel 文件
            try (FileOutputStream fos = new FileOutputStream(excelFilePath)) {
                workbook.write(fos);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
//            pvHcMapper.addData(pvHcInfo);
    }

    @Override
    public PvHcInfoDAO getByAddress(int address) {

        String excelFilePath = "config.xlsx";

        try (InputStream fis = new FileInputStream(excelFilePath);
             Workbook workbook = new XSSFWorkbook(fis)) {

            // 获取第一个工作表
            Sheet sheet = workbook.getSheet("PvHcInfo");
            PvHcInfoDAO hcInfo = new PvHcInfoDAO();

            for (int i = 1; i < sheet.getLastRowNum()+1; i++) {
                Row row = sheet.getRow(i);
                int addressInDate = (int)row.getCell(0).getNumericCellValue();
                if (addressInDate == address){
                    hcInfo.address = (int)row.getCell(0).getNumericCellValue();
                    hcInfo.name = row.getCell(1).getStringCellValue();
                    hcInfo.signed = row.getCell(2).getBooleanCellValue();
                    hcInfo.precision = row.getCell(3).getNumericCellValue();
                    hcInfo.numberOfBytes = (int)row.getCell(4).getNumericCellValue();
                    hcInfo.readOrWrite = row.getCell(5).getStringCellValue();
                    hcInfo.tableNumber = (int)row.getCell(6).getNumericCellValue();
                    hcInfo.describe = row.getCell(7).getStringCellValue();
                }
            }
            // 保存更改到 Excel 文件
            try (FileOutputStream fos = new FileOutputStream(excelFilePath)) {
                workbook.write(fos);
            }
            return hcInfo;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
//        return pvHcMapper.getByAddress(address);
    }

    @Override
    public void deleteByAddress(int address) {
        String excelFilePath = "config.xlsx";

        try (InputStream fis = new FileInputStream(excelFilePath);
             Workbook workbook = new XSSFWorkbook(fis)) {

            // 获取第一个工作表
            Sheet sheet = workbook.getSheet("PvHcInfo");

            for (int i = 1; i < sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                int addressInDate = (int)row.getCell(0).getNumericCellValue();
                if (addressInDate == address){
                    // 删除匹配的行
                    sheet.removeRow(row);

                    // 如果不是最后一行，则将后面的行往上移
                    if (i < sheet.getLastRowNum()) {
                        sheet.shiftRows(i + 1, sheet.getLastRowNum(), -1);
                    }
                    break;
                }
            }
            // 保存更改到 Excel 文件
            try (FileOutputStream fos = new FileOutputStream(excelFilePath)) {
                workbook.write(fos);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

//        pvHcMapper.deleteByAddress(address);
    }

    @Override
    public void changeData(PvHcInfoDAO pvHcInfo) {

        String excelFilePath = "config.xlsx";

        try (InputStream fis = new FileInputStream(excelFilePath);
             Workbook workbook = new XSSFWorkbook(fis)) {

            // 获取第一个工作表
            Sheet sheet = workbook.getSheet("PvHcInfo");

            // todo number值不对，少一个
            for (int i = 1; i < sheet.getLastRowNum() + 1; i++) {
                Row row = sheet.getRow(i);
                int addressInDate = (int)row.getCell(0).getNumericCellValue();
                if (addressInDate == pvHcInfo.address){
                    row.getCell(1).setCellValue(pvHcInfo.name);
                    row.getCell(2).setCellValue(pvHcInfo.signed);
                    row.getCell(3).setCellValue(pvHcInfo.precision);
                    row.getCell(4).setCellValue(pvHcInfo.numberOfBytes);
                    row.getCell(5).setCellValue(pvHcInfo.readOrWrite);
                    row.getCell(6).setCellValue(pvHcInfo.tableNumber);
                    row.getCell(7).setCellValue(pvHcInfo.describe);
                }
            }
            // 保存更改到 Excel 文件
            try (FileOutputStream fos = new FileOutputStream(excelFilePath)) {
                workbook.write(fos);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

//        pvHcMapper.changeData(pvHcInfo);
    }

    @Override
    public List<PvHcInfoDAO> getAllConfig(){

        List<PvHcInfoDAO> allConfig =  new ArrayList();
        try {
//            allConfig = pvHcMapper.getAllConfig();
            String excelFilePath = "config.xlsx";

            try (InputStream fis = new FileInputStream(excelFilePath);
                 Workbook workbook = new XSSFWorkbook(fis)) {

                // 获取第一个工作表
                Sheet sheet = workbook.getSheet("PvHcInfo");

                int lastRowNum = sheet.getLastRowNum();

                // todo number值不对，少一个
                for (int i = 1; i < sheet.getLastRowNum() + 1; i++) {
                    PvHcInfoDAO hcInfo = new PvHcInfoDAO();
                    Row row = sheet.getRow(i);
                    hcInfo.address = (int)row.getCell(0).getNumericCellValue();
                    hcInfo.name = row.getCell(1).getStringCellValue();
                    hcInfo.signed = row.getCell(2).getBooleanCellValue();
                    hcInfo.precision = row.getCell(3).getNumericCellValue();
                    hcInfo.numberOfBytes = (int)row.getCell(4).getNumericCellValue();
                    hcInfo.readOrWrite = row.getCell(5).getStringCellValue();
                    hcInfo.tableNumber = (int)row.getCell(6).getNumericCellValue();
                    hcInfo.describe = row.getCell(7).getStringCellValue();
                    allConfig.add(hcInfo);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return allConfig;
    }


}
